package com.gwl.linkoptionspicker;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.style.cityjd.JDCityConfig;
import com.lljjcoder.style.cityjd.JDCityPicker;
import com.lljjcoder.style.citylist.Toast.ToastUtils;
import com.lljjcoder.style.citypickerview.CityPickerView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

/**
 *
 */
public class MainActivity extends AppCompatActivity {
    //申明对象
    CityPickerView mPicker=new CityPickerView();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.buttonPanel1).setOnClickListener(v -> {
            ArrayList<String> arrayList = new ArrayList<>();
            arrayList.add("第一");
            arrayList.add("第二");
            arrayList.add("第三");
            Utils.initNoLinkOptionsPicker1(arrayList, this, "#606266", "#f7565a");

        });
//========================二级联动==========================================

        findViewById(R.id.buttonPanel2).setOnClickListener(v -> {
            ArrayList<String> arrayList = new ArrayList<>();
            arrayList.add("第一");
            arrayList.add("第二");
            arrayList.add("第三");

            ArrayList<String> arrayList2 = new ArrayList<>();
            arrayList2.add("第一");
            arrayList2.add("第二");
            arrayList2.add("第三");
            Utils.initNoLinkOptionsPicker2(arrayList, arrayList2, this);

        });

//========================二级联动==========================================


//========================三级联动==========================================
        String[] item2 = {"全天", "上午", "下午", "晚上"};
        String[] item3_0 = {"都可以"};
        String[] item3_1 = {"都可以", "00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00"};
        String[] item3_2 = {"都可以", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00"};
        String[] item3_3 = {"都可以", "19:00", "20:00", "21:00", "22:00", "23:00"};

        final ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();
        String[] item1 = getBeforeSevenDay();
        final ArrayList<String> options1Items = new ArrayList<>(Arrays.asList(item1));
        final ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
        for (int z = 0; z < options1Items.size(); z++) {
            options2Items.add( new ArrayList<>(Arrays.asList(item2)));
        }
        ArrayList<ArrayList<String>> options3Items_01;
        ArrayList<String> options3Items_01_01;
        //全部下级的内容数据
        for (int z = 0; z < options1Items.size(); z++) {
            options3Items_01 = new ArrayList<>();
            for (int k = 0; k < item2.length; k++) {
                options3Items_01_01 = new ArrayList<>();
                if (k == 0) {
                    options3Items_01_01.add(item3_0[0]);
                    options3Items_01.add(options3Items_01_01);
                } else if (k == 1) {
                    options3Items_01_01.addAll(Arrays.asList(item3_1));
                    options3Items_01.add(options3Items_01_01);
                } else if (k == 2) {
                    options3Items_01_01.addAll(Arrays.asList(item3_2));
                    options3Items_01.add(options3Items_01_01);
                } else if (k == 3) {
                    options3Items_01_01.addAll(Arrays.asList(item3_3));
                    options3Items_01.add(options3Items_01_01);
                }

            }
            options3Items.add(options3Items_01);
        }

        OptionsPickerView pickerBuilder = new OptionsPickerBuilder(this, (options1, options2, options3, v) -> {
            //判 Null
            if (options1Items.size() > 0) {
                String tx = options1Items.get(options1);
                if (options2Items.size() > 0) {
                    String tx1 = options2Items.get(options1).get(options2);
                    if (options3Items.get(options1).get(options2).size() > 0) {
                        String tx2 = options3Items.get(options1).get(options2).get(options3);
                        Toast.makeText(getBaseContext(), tx + "-" + tx1 + "-" + tx2, Toast.LENGTH_SHORT).show();
                    }
                }
            }
        })
                .setTitleText("请选择")
                .setContentTextSize(25)
                .setDividerColor(Color.LTGRAY)
                .setSelectOptions(0, 0, 0)
                .setBgColor(Color.WHITE)
                .setTitleBgColor(Color.WHITE)
                .setTitleColor(Color.LTGRAY)
                .setCancelColor(getResources().getColor(R.color.black))
                .setSubmitColor(getResources().getColor(R.color.black))
                .setTextColorCenter(R.color.purple_700)
                .isRestoreItem(true)
                .isCenterLabel(false)
                .setOptionsSelectChangeListener((options1, options2, options3) -> {
                    String str = "options1: " + options1 + "\noptions2: " + options2 + "\noptions3: " + options3;
                })
                .build();
        //给选择器添加数据
        pickerBuilder.setPicker(options1Items);//一级选择器
        pickerBuilder.setPicker(options1Items, options2Items);//二级选择器
        pickerBuilder.setPicker(options1Items, options2Items, options3Items);//三级选择器

        findViewById(R.id.buttonPanel3).setOnClickListener(v -> {
            pickerBuilder.show();
        });
//========================三级联动==========================================



//==============================================//预先加载仿iOS滚轮实现的全部数据/==============================================
        mPicker.init(this);
        //添加默认的配置，不需要自己定义，当然也可以自定义相关熟悉，详细属性请看demo
        CityConfig cityConfig = new CityConfig.Builder().build();
        mPicker.setConfig(cityConfig);
//监听选择点击事件及返回结果
        mPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {

                //省份province
                //城市city
                //地区district
            }

            @Override
            public void onCancel() {
                ToastUtils.showLongToast(MainActivity.this, "已取消");
            }
        });
        findViewById(R.id.buttonPanel4).setOnClickListener(v -> {
            mPicker.showCityPicker( );
        });
//==============================================//预先加载仿iOS滚轮实现的全部数据/==============================================



//==============================================//京东白条/==============================================
        JDCityPicker cityPicker = new JDCityPicker();
        JDCityConfig jdCityConfig = new JDCityConfig.Builder().build();

        jdCityConfig.setShowType(JDCityConfig.ShowType.PRO_CITY_DIS);
        cityPicker.init(this);
        cityPicker.setConfig(jdCityConfig);
        cityPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                ToastUtils.showShortToast(MainActivity.this, "城市选择结果：\n" + province.getName() + "(" + province.getId() + ")\n"
                        + city.getName() + "(" + city.getId() + ")\n"
                        + district.getName() + "(" + district.getId() + ")");
            }

            @Override
            public void onCancel() {

            }
        });

        findViewById(R.id.buttonPanel5).setOnClickListener(v -> {
            cityPicker.showCityPicker();
        });
//==============================================//京东白条/==============================================
//==============================================//时间选择/==============================================
        //时间选择器
        initTimePicker();
        findViewById(R.id.buttonPanel6).setOnClickListener(v -> {
            if (pvTime != null) {
                pvTime.show();//弹出时间选择器，传递参数过去，回调的时候则可以绑定此view
            }
        });
//==============================================//时间选择/==============================================



//==============================================//选择开始和结束时间/==============================================
        et_startTime = findViewById(R.id.et_startTime);
        et_endTime = findViewById(R.id.et_endTime);
        et_startTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pvTime != null) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(startTime);
                    pvTime.setDate(calendar);
                    pvTime.show();
                    et_startTimeFlag = true;
                }
            }
        });
        et_endTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pvTime != null) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(endTime);
                    pvTime.setDate(calendar);
                    pvTime.show();
                    et_startTimeFlag = false;

                }
            }
        });
        initTimePicker2();

//==============================================//选择开始和结束时间/==============================================

    }

    boolean et_startTimeFlag = false;
    private void initTimePicker2() {
        pvTime = new TimePickerBuilder(MainActivity.this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                //如果是开始时间的EditText
                if(et_startTimeFlag){
                    startTime = date;
                    et_startTime.setText(getTime(date));
                }else {
                    endTime = date;
                    et_endTime.setText(getTime(date));
                }
            }
        })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {

                    }
                })
                .setType(new boolean[]{true, true, true, true, true, false})
                .isDialog(true)
                .build();


        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
        }
    }

    private EditText et_startTime;
    private EditText et_endTime;

    private Date startTime = new Date();
    private Date endTime = new Date();

    private TimePickerView pvTime;

    public static String[] getBeforeSevenDay() {
        String[] arr = new String[7];
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        Calendar c = null;
        for (int i = 0; i < 7; i++) {
            c = Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, -i - 1);
            arr[6 - i] = sdf.format(c.getTime());

        }
        return arr;
    }

    private void initTimePicker() {//Dialog 模式下，在底部弹出

        pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                Log.i("pvTime", "onTimeSelect");

            }
        })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                        Log.i("pvTime", "onTimeSelectChanged");
                    }
                })
                .setType(new boolean[]{true, true, true, true, true, true})
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
        }
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        Log.d("getTime()", "choice date millis: " + date.getTime());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }
}